﻿using System;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;


namespace shopform
{
    /// <summary>
    /// This class handles database connection.
    /// </summary>
    public sealed class myDatabaseConnection
    {
        //Settings for database connection
        private static String databaseServerAddress = "127.0.0.1";
        private static String databaseName = "shopform";
        private static String databaseUser = "shopform";
        private static String databasePass = "shop1form";

        private static SqlConnection
            myConnection = new SqlConnection();

        static myDatabaseConnection instance = null;
        static readonly object padlock = new object();

        private myDatabaseConnection()
        {
            try
            {
                //creating new connection string builder
                SqlConnectionStringBuilder connStringBuilder =
                        new SqlConnectionStringBuilder();

                //setting up connection details
                connStringBuilder.DataSource = myDatabaseConnection.databaseServerAddress;
                connStringBuilder.InitialCatalog = myDatabaseConnection.databaseName;
                connStringBuilder.UserID = myDatabaseConnection.databaseUser;
                connStringBuilder.Password = myDatabaseConnection.databasePass;

                //adjusting connection object and opening connection
                myConnection.ConnectionString = connStringBuilder.ConnectionString;
                myConnection.Open();
            }
            catch (SqlException e)
            {
                throw new Exception("Sql Exception\n" + e.Message);
            }
        }

        /// <summary>
        /// Only interface to communicate with other classes.
        /// </summary>
        /// <returns>SqlConnection type connection object.</returns>
        public static SqlConnection getConnection()
        {
            //this way we are sure that an instance is always available
            myDatabaseConnection _mdbConn = myDatabaseConnection.Instance;
            
            return myDatabaseConnection.myConnection;
        }


        /// <summary>
        /// Checks if the connection to the database is open.
        /// </summary>
        /// <returns>True if connection is open, false if connection is closed.</returns>
        private static bool connectionIsOpen()
        {
            if (myConnection.State == ConnectionState.Open)
            {
                return true;
            }
            return false;
        }


        /// <summary>
        /// Closes database connection upon calling.
        /// </summary>
        public static void connectionClose()
        {
            try
            {
                if (myDatabaseConnection.connectionIsOpen())
                {
                    myConnection.Close();
                }
            }
            catch (SqlException e)
            {
                throw new Exception("Sql Exception\n" + e.Message);
            }
        }

        /// <summary>
        /// Required for singleton model.
        /// </summary>
        public static myDatabaseConnection Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new myDatabaseConnection();
                    }
                    return instance;
                }
            }
        }


    }
}